APM.ConfigSysbasicController = Ember.Controller.extend({

	needs: ["mainFrame"],

	logo: Ember.computed.alias('controllers.mainFrame.logo'),

	init: function(){
		this._super();
		this.initConfData();
	},
	
	form: {
		apdex_2: "2",
		logo:"",
		confType:"1"
	},
							
	checkFormValid: function(){
		var form = this.get("form");

		var apdex_t = this.get("apdex_t");
		if (form.apdex_t && !/^[\d]+$|^\d*\.\d+$/.test(form.apdex_t)) {
			this.set("formTip", "apdex_t值必须为整数或小数！");
			return;
		}
		
		this.set("formTip", null);
		return true;
	},
	
	initConfData: function(){
		var ctrl = this;
		var form = this.get("form");
		ctrl.store.ajax("GET", "configs/systemConfig",{
			configItem:"basic"
		}).then(function(response){
			var configobj=response.configData;
			if(configobj){
				if(configobj.basic_apdex_t){
					ctrl.set("form.apdex_t",configobj.basic_apdex_t);
				}
				ctrl.set("form.logo",configobj.basic_logo);
			}	
		});
	},

	actions: {
		reset : function() {
			this.set("formTip", null);
		},
		
		cancel:function(){
			var ctrl = this;
			confirmDialog("确定恢复默认配置?")
			.then(function() {				
				var form = ctrl.get("form");
				ctrl.store.ajax("POST", "configs/systemConfig", {
					apdex_t: "2",
					confType:form.confType,
					resetToFactory: true
				}).then(function(response){
					alertTip("重置成功！");
					ctrl.initConfData();
					var ctrl = this;	
					APM.iframeCallback = function(status) {
						var logo = ctrl.get('logo').replace(/\?.*$/, '');
						ctrl.set('logo', logo+'?v='+Math.random());
						delete APM.iframeCallback;
					};
					var form1 = $('#sysform');
					form1[0].method="POST";
					form1[0].action="../front/rest/apm/configs/logo?token="+encodeURIComponent($.cookie("token"));
					form1[0].submit();
				});
			});
		},

		save: function(){
			if( !this.checkFormValid() ){
				return;
			}
			var ctrl = this;

			APM.iframeCallback = function(status) {
				var logo = ctrl.get('logo').replace(/\?.*$/, '');

				ctrl.set('logo', logo+'?v='+Math.random());
				switch(status){
					case 2:
						alertTip('图片应为png格式！');
						break;
					case 3:
						alertTip('文件尺寸过大！');
						break;
					case 4:
						alertTip('文件格式受限制且尺寸过大！');
						break;
					case 6:
						alertTip('上传失败,出现异常！');
						break;
					case 8:
						alertTip('Apdex_T保存失败！');
						break;
					case 1:
						//文件上传成功！
					case 5:
						//没有选择上传文件！
					case 7:
						//Apdex_T保存成功！
						alertTip('保存成功！');
						ctrl.initConfData();
						break;
					default:
						alertTip('保存失败');
						break;
				}
				delete APM.iframeCallback;
			};

			var form1 = $('#sysform');
			form1[0].method="POST";
			form1[0].action="../front/rest/apm/configs/logo?token="+encodeURIComponent($.cookie("token"));
			form1[0].submit();
		}
	}
});